<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <span>姓名:</span>
            <input type="text" v-model="form.name" />
        </div>
        <div>
            <span>年龄:</span>
            <input type="number" v-model.number="form.age" />
        </div>
        <div>
            <span>性别:</span>
            <select v-model="form.gender">
                <option value="保密">保密</option>
                <option value="男">男</option>
                <option value="女">女</option>
                <option value="无性别">无性别</option>
                <option value="武装直升机">武装直升机</option>
                <option value="沃尔玛购物带">沃尔玛购物带</option>
                <option value="码农">码农</option>
                <option value="畜生">畜生</option>
                <option value="生理男,心理男,想象女">生理男,心理男,想象女</option>
            </select>
        </div>
        <div>
            <button @click="addOrUpdate">添加/修改</button>
        </div>
        <div>
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(item, index) in list" :key="item.id">
                    <td>{{ index + 1 }}</td>
                    <td>{{ item.name }}</td>
                    <td>{{ item.age }}</td>
                    <td>{{ item.gender }}</td>
                    <td>
                        <button @click="deleteItem(index)">删除</button>
                        <button @click="editItem(index)">编辑</button>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <script src="./vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                form: {
                    name: '',
                    age: null,
                    gender: '保密'
                },
                list: [],
                editIndex: -1 // 用于记录当前编辑的索引
            },
            methods: {
                addOrUpdate() {
                    if (this.editIndex !== -1) {
                        // 修改
                        this.list[this.editIndex] = { ...this.form };
                        this.editIndex = -1;
                    } else {
                        // 添加
                        this.list.push({ ...this.form, id: Date.now() });
                    }
                    this.clearForm();
                },
                deleteItem(index) {
                    this.list.splice(index, 1);
                },
                editItem(index) {
                    this.editIndex = index;
                    this.form = { ...this.list[index] };
                },
                clearForm() {
                    this.form = {
                        name: '',
                        age: null,
                        gender: '男'
                    };
                }
            }
        });
    </script>
</body>

</html>